﻿'************************************************************************************'
'* Copyright (c) 2009, Patrick Driscoll All rights reserved.                        *'
'*                                                                                  *'
'* Redistribution and use in source and binary forms, with or without               *'
'* modification, are permitted provided that the following conditions are met:      *'
'*                                                                                  *'
'* Redistributions of source code must retain the above copyright notice, this      *'
'* list of conditions and the following disclaimer. Redistributions in binary       *'
'* form must reproduce the above copyright notice, this list of conditions and      *'
'* the following disclaimer in the documentation and/or other materials             *'
'* provided with the distribution. Neither the name of the DriscollArt Software nor *'
'* the names of its contributors may be used to endorse or promote products         *'
'* derived from this software without specific prior written permission.            *'
'*                                                                                  *'
'* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"      *'
'* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE        *'
'* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE       *'
'* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR      *'
'* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL           *'
'* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR       *'
'* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER       *'
'* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT               *'
'* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY        *'
'* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH      *'
'* DAMAGE.                                                                          *'
'************************************************************************************'

Imports System.Data.SqlClient

''' <summary>
''' Code preview & save dialog
''' </summary>
''' <remarks></remarks>
Public Class Dialog_PreviewSection

    Private _DatabaseProperties As DataTable
    Private _DNNProperties As Collection
    Private Sub Button_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Cancel.Click
        Me.Close()
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub New(ByVal DatabaseProperties As DataTable, ByVal dnnProperties As Collection)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        _DatabaseProperties = DatabaseProperties
        _DNNProperties = dnnProperties
    End Sub
  
    Private Sub Dialog_Preview_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim DirInfo_Templates As New System.IO.DirectoryInfo(Application.StartupPath & "\templates")

        For Each objFileInfo As System.IO.FileInfo In DirInfo_Templates.GetFiles
            If objFileInfo.Extension = ".template" Then
                ComboBox_Template.Items.Add(objFileInfo.Name)
            End If
        Next

    End Sub

    Private Sub Button_Clipboard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Clipboard.Click
        My.Computer.Clipboard.SetText(TextBox_Output.Text)
        MsgBox("Script copied to clipboard!", MsgBoxStyle.OkOnly, "Clipboard")
    End Sub


    Private Sub Button_PreviewCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_PreviewCode.Click
        If ComboBox_Template.SelectedIndex = -1 Then
            MsgBox("Please select a template file.", MsgBoxStyle.OkOnly, "Select Template")
            Exit Sub
        End If

        TextBox_Output.Text = GetTemplateCode("templates\" & ComboBox_Template.Text)
    End Sub

    Private Function GetTemplateCode(ByVal TemplateFileName As String) As String
        Dim objGenerator As New cVBEvalProvider
        Dim sbCode As New System.Text.StringBuilder
        Dim objStreamReader As New System.IO.StreamReader(New System.IO.FileStream(System.AppDomain.CurrentDomain.BaseDirectory & TemplateFileName, IO.FileMode.Open))
        Do Until objStreamReader.EndOfStream
            sbCode.AppendLine(objStreamReader.ReadLine)
        Loop
        objStreamReader.Close()

        Return objGenerator.GenerateDNNCode(sbCode.ToString, _DNNProperties, _DatabaseProperties)
    End Function

    Private Sub SaveTemplateCode(ByVal TemplateFileName As String)
        Dim objGenerator As New cVBEvalProvider
        Dim sbCode As New System.Text.StringBuilder
        Dim objStreamReader As New System.IO.StreamReader(New System.IO.FileStream(System.AppDomain.CurrentDomain.BaseDirectory & TemplateFileName, IO.FileMode.Open))
        Do Until objStreamReader.EndOfStream
            sbCode.AppendLine(objStreamReader.ReadLine)
        Loop
        objStreamReader.Close()

        objGenerator.SaveDNNCode(sbCode.ToString, _DNNProperties, _DatabaseProperties)
    End Sub

    Private Sub Button_SaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_SaveFile.Click
        If Main.TextBox_RootDNNPath.Text.Trim = String.Empty Then
            MsgBox("Please specifiy a root DNN path in the main window before trying to save generated code.", MsgBoxStyle.OkOnly, "Root Path Missing")
            Exit Sub
        End If

        If ComboBox_Template.SelectedIndex = -1 Then
            MsgBox("Please select a template file.", MsgBoxStyle.OkOnly, "Select Template")
            Exit Sub
        End If

        If MsgBox("This file will be generated with its default name & location under" & vbCrLf & Main.TextBox_RootDNNPath.Text & vbCrLf & "Is this OK?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            TextBox_Output.Text = GetTemplateCode("templates\" & ComboBox_Template.Text)
            SaveTemplateCode("templates\" & ComboBox_Template.Text)
        End If
    End Sub
End Class